package com.example.my_dubbo.loadBalance;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author 何可人
 * @version 1.0
 * @description: TODO
 * @date 2022/12/9 10:31
 */
public class WeightRandomLoadBalance {  // 按权重进行复制

    public static void main(String[] args) {
        for(int i = 0;i<10;i++){
            System.out.println(getServerName());
        }
    }

    public static String getServerName(){
        List<String> ips = new ArrayList<>();
        for(String ip:ServerIps.WEIGHT_LIST.keySet()){
            int weight = ServerIps.WEIGHT_LIST.get(ip);
            for(int i = 0;i<weight;i++){
                ips.add(ip);
            }
        }
        Random random = new Random();
        int index = random.nextInt(ips.size());
        return ServerIps.LIST.get(index);
    }
}
